var map = (function() {
	var map;
	var markers = [];
	var infoWindows = [];
	var geoCache = {};
	var geocoder = new google.maps.Geocoder();

	var defaults = {
		evoData : null,
		citiesData : null
	}

	var options;

	var setOptions = function(o) {
		options = $.extend({}, defaults, o);
	}

	var render = function() {
		initializeMap();
	}

	var update = function(chosenValue) {
		updateMapInfo(chosenValue);
	}

	function initializeMap() {
		var mapOptions = {
			zoom : 2,
			center : new google.maps.LatLng(53.54, 27.34),
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
	}

	function showCities(cities) {
	    for ( var i = 0, len = cities.length; i < len; i++) {
            if (cities[i].employeesNumber > 0)
                showCity(cities[i]);
        }
	}

	function showCity(city) {
        var marker = new google.maps.Marker({
            position : new google.maps.LatLng(city.latitude, city.longitude),
            map : map,
            title : city.cityName
        });

        var infowindow = new google.maps.InfoWindow({
            content : city.cityName + ":" + city.employeesNumber
        });

        infoWindows.push(infowindow);

        google.maps.event.addListener(marker, 'click', function() {
            closeInfoWindows();
            infowindow.open(map, marker);
        });

        markers.push(marker);
        marker.setMap(map);
	}

//	function addMarkers(cities) {
//		for ( var i = 0, len = cities.length; i < len; i++) {
//			if (cities[i].employeesNumber > 0)
//				showCityOnMap(cities[i]);
//		}
//	}
//
//	function showCityOnMap(city, callback) {
//		var cityName = city.cityName;
//		if (cityName in options.citiesData) {
//			var pos = new google.maps.LatLng(options.citiesData[cityName].pos.ob, options.citiesData[cityName].pos.pb);
//			callback(pos, city);
//		} else if (cityName in geoCache) {
//			callback(geoCache[cityName], city);
//		} else {
//			geocoder.geocode({
//				'address' : cityName
//			}, function(results, status) {
//				if (status == google.maps.GeocoderStatus.OK) {
//					var pos = results[0].geometry.location;
//					geoCache[cityName] = pos;
//					callback(pos, city);
//				} else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
//					setTimeout(function() {
//						showCityOnMap(city, callback);
//					}, 200);
//				} else {
//					console.log("Geocode was not successful for the following reason: " + status);
//				}
//			});
//		}
//	}
//
//	function addMarker(pos, city) {
//		var marker = new google.maps.Marker({
//			position : pos,
//			map : map,
//			title : city.cityName
//		});
//
//		var infowindow = new google.maps.InfoWindow({
//			content : city.cityName + ":" + city.workers
//		});
//
//		infoWindows.push(infowindow);
//
//		google.maps.event.addListener(marker, 'click', function() {
//			closeInfoWindows();
//			infowindow.open(map, marker);
//		});
//
//		markers.push(marker);
//		marker.setMap(map);
//	}

	function clearOverlays() {
		for ( var i = 0, len = markers.length; i < len; i++) {
			markers[i].setMap(null);
		}
		markers = [];
	}

	function closeInfoWindows() {
		for ( var i = 0, len = infoWindows.length; i < len; i++) {
			infoWindows[i].close();
		}
	}

	function updateMapInfo(quarterNumber) {
        $.getJSON("/restService/city-loc", {quarter:quarterNumber}).done(function(data) {
            chosenDate = data.year + " year, " + data.quarter + " quarter";

            $("#currData").html(chosenDate);
            showCities(data.cities);
        });

        clearOverlays();
	}

	return {
		setOptions : setOptions,
		render : render,
		update : update,
		geocoder : geocoder
	}
})()